package com.imodia.checkoo.webtag;

import java.io.IOException;
import java.io.Writer;

import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.hsq.commons.util.BeanFactory;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.PageResult;
import com.imodia.checkoo.lucence.LucenceDataBean;
import com.imodia.checkoo.lucence.search.SearchLucenceFile;

public class LucencePageTag extends RequestContextAwareTag  {
	private static final long serialVersionUID = -1764906844122921796L;
	private TldParam tldParam;
	private PageBean pageParam;

	public TldParam getTldParam() {
		return tldParam;
	}
	public void setTldParam(TldParam tldParam) {
		this.tldParam = tldParam;
	}
	public PageBean getPageParam() {
		return pageParam;
	}
	public void setPageParam(PageBean pageParam) {
		this.pageParam = pageParam;
	}

	@Override
	protected int doStartTagInternal() {
		SearchLucenceFile searchLucenceFile = (SearchLucenceFile) BeanFactory.getBean("searchLucenceFile");
		PageResult<LucenceDataBean> pageList = new PageResult<LucenceDataBean>();
		pageList.setCurrentPage(pageParam.getCurrentPage());
		pageList.setPageSize(tldParam.getEndNum());
		try {
			searchLucenceFile.setFilePath(pageContext.getRequest().getRealPath("/"));
			searchLucenceFile.doSearch(tldParam.getQueryWord(), "1", pageList);
		} catch (Exception e) {
				e.printStackTrace();
		}
		pageContext.setAttribute("LucencePageTag", pageList);
		
		Writer out = pageContext.getOut();
		StringBuffer buf = new StringBuffer();
		try {
			if (tldParam.getHtmls() != null && tldParam.getHtmls().trim().length() > 0) {
				buf.append(TldUtil.convertLucence(pageList.getData(), tldParam));
			}
			buf.append("<input name='totalSize' type='hidden' id='totalSize' value='"+pageList.getTotalSize()+"' />");
			buf.append("<input name='pageSize' type='hidden' id='pageSize' value='"+pageList.getPageSize()+"' />");
			buf.append("<input name='pages' type='hidden' id='pages' value='"+pageList.getPages()+"' />");
			buf.append("<input name='currentPage' type='hidden' id='currentPage' value='"+pageList.getCurrentPage()+"' />");
			out.write(buf.toString());
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out = null;
		return EVAL_BODY_INCLUDE;
	}

}
